﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;
using Omu.ValueInjecter;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" Supplier to change the class name "SupplierServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class SupplierServices : ISupplierServices
    {
        private readonly SupplierRepository SupplierRepository;
        private WcfSessionStorage _session;
        public SupplierServices()
        {
            _session = new WcfSessionStorage();
            SupplierRepository = new SupplierRepository(_session);
        }

        #region ISupplierServices Members

        public List<SupplierDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                         string sortType = "ASC")
        {
            var Suppliers = new List<Supplier>();
            var Results = new List<SupplierDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Suppliers = SupplierRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Suppliers = SupplierRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = SupplierRepository.RecordCount;

            Suppliers.ForEach(x =>
                                  {
                                      var supplierDto = new SupplierDTO();
                                      supplierDto.InjectFrom(x);
                                      supplierDto.Coa = new CoaDTO();
                                      if (x.Coa != null) supplierDto.Coa.InjectFrom(x.Coa);
                                      Results.Add(supplierDto);
                                  }
                );

            return Results;
        }

        public bool Save(SupplierDTO SupplierDTO,String mode)
        {
            bool res;
            try
            {
                Supplier Supplier = SupplierRepository.GetDataOne(x => x.SupplierId == SupplierDTO.SupplierId);
                if (mode == "Add")
                    if (Supplier != null)throw  new Exception("Supplier Sudah Ada!");
                var user = new User();
                if (Supplier == null)
                {
                    Supplier = new Supplier();
                    SupplierDTO.created_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(SupplierDTO.created_by, user);
                    //Supplier.modified_by = null;
                }
                else
                {
                    SupplierDTO.modified_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(SupplierDTO.modified_by, user);
                }
                Mapper.Map(SupplierDTO, Supplier);
                SupplierRepository.SaveOrUpdate(Supplier);
                res = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return res;
        }


        public bool Delete(SupplierDTO SupplierDto)
        {
            bool res = false;
            try
            {
                var Supplier = new Supplier();
                Mapper.Map(SupplierDto, Supplier);
                PurchaseOrderRepository purchaseOrderRepository = new PurchaseOrderRepository(_session);
                var PO = purchaseOrderRepository.GetDataOne(x => x.Supplier.SupplierId == Supplier.SupplierId);
                if (PO != null) return res;
                SupplierRepository.Delete(Supplier);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<SupplierDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                   out long recordCount)
        {
            var Suppliers = new List<Supplier>();
            var Results = new List<SupplierDTO>();
            Suppliers = SupplierRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = SupplierRepository.RecordCount;
            Suppliers.ForEach(x =>
                                  {
                                      var supplierDto = new SupplierDTO();
                                      supplierDto.InjectFrom(x);
                                      supplierDto.Coa = new CoaDTO();
                                      if (x.Coa != null) supplierDto.Coa.InjectFrom(x.Coa);
                                      Results.Add(supplierDto);
                                  }
                );

            return Results;
        }


        public List<SupplierDTO> GetAllSupplier()
        {
            List<Supplier> Suppliers = SupplierRepository.FetchAll().ToList();
            var Results = new List<SupplierDTO>();
            Suppliers.ForEach(x =>
                                  {
                                      var supplierDto = new SupplierDTO();
                                      supplierDto.InjectFrom(x);
                                      supplierDto.Coa = new CoaDTO();
                                      if (x.Coa != null) supplierDto.Coa.InjectFrom(x.Coa);
                                      Results.Add(supplierDto);
                                  }
                );

            return Results;
        }


        public SupplierDTO GetSupplier(String SupplierId)
        {
            Supplier Supplier = SupplierRepository.GetDataOne(x => x.SupplierId == SupplierId);
            var supplierDto = new SupplierDTO();
            supplierDto.InjectFrom(Supplier);
            supplierDto.Coa = new CoaDTO();
            if (Supplier.Coa != null) supplierDto.Coa.InjectFrom(Supplier.Coa);
            return supplierDto;
        }

        public List<SupplierDTO> GetSupplierList(string SupplierName)
        {
            List<Supplier> Suppliers =
                SupplierRepository.GetDataList(x => x.SupplierName.Contains(SupplierName)).ToList();
            var Results = new List<SupplierDTO>();
            Suppliers.ForEach(x =>
                                  {
                                      var supplierDto = new SupplierDTO();
                                      supplierDto.InjectFrom(x);
                                      supplierDto.Coa = new CoaDTO();
                                      if (x.Coa != null) supplierDto.Coa.InjectFrom(x.Coa);
                                      Results.Add(supplierDto);
                                  }
                );

            return Results;
        }

        #endregion
    }
}